﻿Public Class frmDateTime

    Private m_dateNow As DateTime
    Private lblInput As TenKeyLabel
    Private fIsLabelInput As Boolean = False

    Public Sub New()

        ' この呼び出しはデザイナーで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        fIsLabelInput = False
    End Sub

    Public Sub New(ByRef InputLabel As TenKeyLabel)

        ' この呼び出しはデザイナーで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後で初期化を追加します。

        lblInput = InputLabel
        fIsLabelInput = True
    End Sub

    Private Sub frmDateTime_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_dateNow = DateTime.Now()
        lblDateTime.Text = m_dateNow.ToString(frmStartCompleteBase.DATE_TIME_FORMAT)

        Dim i As Integer
        For i = m_dateNow.Year - 3 To m_dateNow.Year + 3
            cbYear.Items.Add(i)
        Next

        For i = 1 To 12
            cbMonth.Items.Add(i)
        Next

        For i = 1 To DateTime.DaysInMonth(m_dateNow.Year, m_dateNow.Month)
            cbDay.Items.Add(i)
        Next

        For i = 0 To 23
            cbHour.Items.Add(i)
        Next

        For i = 0 To 59
            cbminute.Items.Add(i)
        Next

        cbYear.SelectedItem = m_dateNow.Year
        cbMonth.SelectedItem = m_dateNow.Month
        cbDay.SelectedItem = m_dateNow.Day
        cbHour.SelectedItem = m_dateNow.Hour
        cbMinute.SelectedItem = m_dateNow.Minute

        btnConfirm.DialogResult = Windows.Forms.DialogResult.OK
        btnCancel.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cbYear_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbYear.SelectionChangeCommitted
        cbDay.Items.Clear()
        Dim i As Integer
        For i = 1 To DateTime.DaysInMonth(cbYear.SelectedItem, cbMonth.SelectedItem)
            cbDay.Items.Add(i)
        Next
        If m_dateNow.Day > DateTime.DaysInMonth(cbYear.SelectedItem, cbMonth.SelectedItem) Then
            cbDay.SelectedItem = DateTime.DaysInMonth(cbYear.SelectedItem, cbMonth.SelectedItem)
        Else
            cbDay.SelectedItem = m_dateNow.Day
        End If

        SetDate()
    End Sub

    Private Sub cbMonth_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbMonth.SelectionChangeCommitted
        cbDay.Items.Clear()
        Dim i As Integer
        For i = 1 To DateTime.DaysInMonth(cbYear.SelectedItem, cbMonth.SelectedItem)
            cbDay.Items.Add(i)
        Next
        If m_dateNow.Day > DateTime.DaysInMonth(cbYear.SelectedItem, cbMonth.SelectedItem) Then
            cbDay.SelectedItem = DateTime.DaysInMonth(cbYear.SelectedItem, cbMonth.SelectedItem)
        Else
            cbDay.SelectedItem = m_dateNow.Day
        End If

        SetDate()
    End Sub

    Private Sub cbDay_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbDay.SelectionChangeCommitted
        SetDate()
    End Sub

    Private Sub cbHour_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbHour.SelectionChangeCommitted
        SetDate()
    End Sub

    Private Sub cbMinute_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbMinute.SelectionChangeCommitted
        SetDate()
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        If fIsLabelInput Then
            lblInput.Text = m_dateNow.ToString("yyyy/MM/dd HH:mm")
        Else
            g_szSelectedTime = m_dateNow.ToString("yyyy/MM/dd HH:mm")
        End If
    End Sub

    Private Sub SetDate()
        m_dateNow = CDate(String.Format("{0}/{1}/{2} {3}:{4}", _
                                      cbYear.SelectedItem, cbMonth.SelectedItem, cbDay.SelectedItem, cbHour.SelectedItem, cbMinute.SelectedItem))
        lblDateTime.Text = m_dateNow.ToString(frmStartCompleteBase.DATE_TIME_FORMAT)
    End Sub
End Class